package features.advance.jdk.stream;

import features.advance.demo.AESUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author LIN
 * @date 2022-05-15 13:39
 */
public class FileStream {

    public static void main(String[] args) {
        String fileName = "D:\\project-2022\\yqjr\\pythonProject\\a\\0tB1ltfq.ts";
        String copyFileName = "C:\\Users\\LIN\\Desktop\\0tB1ltfq.ts";;
        String key = "cc3cd2568a8c899e";
        decryptFile(fileName,key,copyFileName);
    }
    public static void decryptFile(String fileName,String key,String copyFileName){
        File file = new File(fileName);
        File copyFile = new File(copyFileName);
        try(FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(copyFile);){
            byte[] data = new byte[1024*1024];
            int r = -1;
            while((r=fileInputStream.read(data))!=-1){
                byte[] aesDecry = AESUtils.aesDecry(data, key);
                fileOutputStream.write(aesDecry,0,r);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static String readFileData(String fileName){
        File file = new File(fileName);
        try(FileInputStream fileInputStream = new FileInputStream(file);){
            byte[] data = new byte[1024*1024];
            int r = -1;
            StringBuilder sb = new StringBuilder();
            while((r=fileInputStream.read(data))!=-1){
                sb.append(new String(data,0,r, StandardCharsets.UTF_8));
            }
            System.out.println(sb);
            return sb.toString();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
